package common

import (
	"fmt"
	"github.com/smartwalle/alipay/v3"
	"strconv"
	"weee/models"
)

type Pay interface {
	Payment(order models.Order3) (string, error)
}
type AliPay struct {
	AppId      string
	PrivateKey string
	PublicKey  string
}

var ali AliPay
var Client *alipay.Client

func init() {
	var err error
	ali.AppId = "9021000131622561"
	ali.PrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQChYZC9RxHqyGcr+Ok193TPe2Q+o2HkieFwVNqCY+Phnlfj9bfiBtZY5HSNYBXheOR0LwHWxv5zzkbWn/QhW8hxZ6RRcy6fi0Rjgxiol65J8lvZhhjIdedPqNP4iKQdBkDFq9aYrGWKs+ihsQcaea5YKJacPn6Ut1GLlTNgd0B4tW8Y6MG2mksfUHKbEEnvoXEOOnfsJUeWPwoK4Xy+qdGmImuOBLXe8I4ts3XmuZehhihKHzvZKiq5asas9vyurN78cqwPgx+LDXyzu/La0KN/laecszW1ge284X0EjgcNKQ6GCjMOSMWrPmuS2kw8aSM85H69duH3IWBR8bc2KhlBAgMBAAECggEAICRrjJdkJSwHCIYKBPqlZFHCNIucCsW/hf0QjFnS4HQGM1wUZMtR0+CkqifSE9ddQRq/tgRqV6xlBYhUOnSqZpPq8WIMjjBRzbQlrCaRyx6fVqLTutFo4b/RgDXO4g0Fx5kjI8NqcgbFNbcpoAdORzbGUJZN5dbhQfjUg8kcqS3AW0iVIZUSLsyMnPcxXN1dx3KqZ5OTKo/LPCQIiacR6BpUSJ0QFSqviDYjuC09KXg11SOibIfTmB/zaoYAy04XDJiG0yr3IEOfcIQry/dSpG/SXtx7cUjQIPNFHx68LGOxyieU6ocHXa4eyCzrKA8UWz+OWahEPIZ8GuV6lvhWhQKBgQDjP3GDf9Ss8NyboQQuy29pH5Orn7mHMKxhbkzWsUTDd9hYvD/Ji+wALipMlk8ZfrK1xDqH1tkMgGb2guVpsCz8xIdw4vIBo/iFZfH7kUYkX+4SyiC4DrTjKiAIgjud19FB0OhFTXzWz6gw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gw5o0H+nXPLMfsSdLeMh40E5MjtmN471PBy+bAwCyQIDAQAB"
	Client, err = alipay.New(ali.AppId, ali.PrivateKey, false)
	if err != nil {
		fmt.Println(err)
		return
	}
	err = Client.LoadAliPayPublicKey(ali.PublicKey)
	if err != nil {
		fmt.Println(err)
		return
	}
}

func (a *AliPay) Payment(order models.Order3) (string, error) {
	money := strconv.FormatFloat(order.Price, 'f', -1, 64)
	var p = alipay.TradeWapPay{}
	p.NotifyURL = "http://35da4511.r20.cpolar.top/order/pay"
	p.ReturnURL = "http://xxx"
	p.Subject = "杨梅的订单"
	p.OutTradeNo = order.OrderSn
	p.TotalAmount = money
	p.ProductCode = "FAST_INSTANT_TRADE_PAY"

	var url, err = Client.TradeWapPay(p)
	if err != nil {
		fmt.Println(err)
	}
	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	fmt.Println(payURL)
	return payURL, nil
}
